require(["config"],function(){
	require(["jquery","template","header","cookie"],function($,template){
		function Cart(){
			this.products = null;
			this.load();
			this.addListener();
		}
		Cart.prototype = {
			constructor:Cart,
			load:function(){
				$.cookie.json = true;
				var products = this.products = $.cookie("cart")||[];
				if(products.length===0){
					$(".cart-img").show().siblings(".remove").hide();
					return;
				}
				$(".cart-img").hide().siblings(".remove").show();
				var data = {products:products}
				var html = template("li-template",data);
				$(".list").html(html);
			},
			addListener:function(){
				$(".list").on("click",".del-btn",$.proxy(this.delHandel,this));
				$(".list").on("click",".sub,.add",$.proxy(this.amountHandel,this));
				$(".list").on("blur",".amount-text",$.proxy(this.inputHandel,this));
				$(".list").on("click",".ck-btn",$.proxy(this.oneHandel,this));
				$(".product-container").on("click",".checkall-btn",$.proxy(this.allHandel,this));
			},
			delHandel:function(event){
				var src = event.target;
				var li = $(src).parents(".item");
				var id = li.find(".prod-id").attr("alt");
				this.products = this.products.filter(function(prod){
					return !(prod.id==id);
				});
				$.cookie("cart",this.products,{expires:7,path:"/"});
				li.remove();
				this.totalHandel();
				if(this.products.length===0){
					$(".cart-img").show().siblings(".remove").hide();
				}
				$("#cart-num").html(this.products.length);
			},
			amountHandel:function(event){
				var src = event.target;
				var li = $(src).parents(".item");
				var id = li.find(".prod-id").attr("alt");
				var product = this.products.filter(prod=>prod.id==id)[0];
				if($(src).is(".sub")){
					if(product.amount<=1)
						return;
					product.amount--;
				}else{
					product.amount++;
				}
				$.cookie("cart",this.products,{expires:10,path:"/"});
				li.find(".amount-text").val(product.amount);
				this.totalHandel();
			},
			inputHandel:function(event){
				var src = event.target;
				var li = $(src).parents(".item");
				var id = li.find(".prod.id").attr("alt");
				var product = this.products.filter(prod=>id==prod.id)[0];
				var inputAmount = $(src).val();
				var reg = /^[1-9]\d*$/;
				if(!reg.test(inputAmount)){
					$(src).val(product.amount);
					return;
				}
				product.amount = inputAmount;
				$.cookie("cart","this.products",{expires:10,path:"/"});
				this.totalHandel();
			},
			oneHandel:function(event){
				var src = event.target;
				var all = $(".checkall-btn");
				$(src).toggleClass("checked");
				if($(".checked").length==this.products.length){
					all.addClass("all-checked");
				}else{
					all.removeClass("all-checked");
				}
				this.totalHandel();
			},
			allHandel:function(){
				var checkAll = $(".checkall-btn");
				var checks = $(".ck-btn");
				if(checkAll.hasClass("all-checked")){
					checkAll.removeClass("all-checked");
					checks.removeClass("checked");
				}else{
					checkAll.addClass("all-checked");
					checks.addClass("checked");
				}
				this.totalHandel();
			},
			totalHandel:function(){
				var sum = 0;
				$(".checked").each(function(){
					var amt = $(this).parents(".item").find(".amount-text").val();
					var prc = $(this).parents(".item").find(".price-num").text();
					sum += amt*prc;
				});
				$(".money").text(sum.toFixed(2));
				$(".checked-num").text($(".checked").length);
			}
		}
		new Cart();
	});
});